/*
 *  Licensed to PECATS under one or more contributor license agreements below:
 *  1. http://www.mozilla.org/MPL/MPL-1.1.html
 *  2. http://www.gnu.org/licenses/gpl-2.0.html
 *  3. http://www.gnu.org/licenses/lgpl-2.1.html
*/

/* **********************************************************
*  Servlet for managing Customer Information
*  Name: CustomerManagerServlet.java
*  Author: www.pecats.com
*  Date: September 15, 2008
************************************************************/

package com.pecats.server.core;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletConfig;
import java.io.IOException;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import com.pecats.server.core.CustomerAccountManager;
import com.pecats.server.core.Customer;
import com.pecats.server.data.ConfigData;
import com.pecats.server.util.ParseXMLFile;

/**
 * Servlet used to manage various request (create, update, delete, retrive) from the customer
 */
public class CustomerManagerServlet extends HttpServlet 
{

	RequestDispatcher dispatcher;
	public static String CONFIGDIR = "server-properties";
	public static String PATHSEPERATOR = "\\";
	public static String CONFIGFILE = "config.xml";

    /*
     * Method used to initialize CustomerManagerServlet
     * @param config ServletConfig object
     * @exception ServletException
    */
    public void init(ServletConfig config) throws ServletException 
    {
		super.init(config);
		ParseXMLFile parseFile = new ParseXMLFile();
		String path = getServletContext().getRealPath("/") + CONFIGDIR + PATHSEPERATOR + CONFIGFILE;
		parseFile.parseXML(path);
    }

    /*
     * Method used to handle incoming http request.
     * @param request HttpServletRequest object from the incoming request
     * @param response HttpServletResponse object which forms the response
     * @exception ServletException
     * @exception IOException
    */
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response)
            throws ServletException, IOException 
	{		
		String action = (String) request.getParameter("action");
		Long customerid;
		request.setAttribute("Email", (request.getParameter("Email")));	
	
		if (action.equalsIgnoreCase("create"))
		{
			try
			{
	            // Handle actions
				Customer customer = new Customer();		
				customer.setFirstname(request.getParameter("Firstname"));	
				customer.setLastname(request.getParameter("Lastname"));	
				customer.setAddress(request.getParameter("Address"));	
				customer.setCity(request.getParameter("City"));	
				customer.setCountry(request.getParameter("Country"));	
				customer.setPhone(Integer.parseInt(request.getParameter("Phone")));	
				customer.setCompany(request.getParameter("Company"));	
				customer.setEmail(request.getParameter("Email"));	
				customer.setPassword(request.getParameter("Password"));	

				CustomerAccountManager customManager = new CustomerAccountManager();
				customerid = customManager.createCustomer(customer);
				request.setAttribute("customerid", customerid);
		
				if (	ConfigData.payment == "Y" )
				{
					dispatcher = getServletContext().getRequestDispatcher("/PaymentJSP.jsp");		
					dispatcher.forward(request, response);
				}
				else
				{
					dispatcher = getServletContext().getRequestDispatcher("/accountmanager");		
					dispatcher.forward(request, response);
				}

           		} 
			catch (Exception ex) 
			{
	            	throw new ServletException(ex);
        		}
		}
		else
			if (action.equalsIgnoreCase("update"))
			{
			            // Handle actions
					Customer customer = new Customer();		
					customer.setFirstname(request.getParameter("Firstname"));	
					customer.setLastname(request.getParameter("Lastname"));	
					customer.setAddress(request.getParameter("Address"));	
					customer.setCity(request.getParameter("City"));	
					customer.setCountry(request.getParameter("Country"));	
					customer.setPhone(Integer.parseInt(request.getParameter("Phone")));	
					customer.setCompany(request.getParameter("Company"));	
					customer.setEmail(request.getParameter("Email"));	
					customer.setPassword(request.getParameter("Password"));	

					CustomerAccountManager customManager = new CustomerAccountManager();
					customManager.updateCustomer(customer);
					dispatcher = getServletContext().getRequestDispatcher("/PaymentJSP.jsp");		
					dispatcher.forward(request, response);
	
			}
		else			
			if (action.equalsIgnoreCase("delete"))
			{
			            // Handle actions	
					CustomerAccountManager customManager = new CustomerAccountManager();
					customManager.deleteCustomer(request.getParameter("Email"));
					dispatcher = getServletContext().getRequestDispatcher("/PaymentJSP.jsp");		
					dispatcher.forward(request, response);	
			}
		else			
			if (action.equalsIgnoreCase("retrieve"))
			{
			            // Handle actions	
					request.getParameter("Email");
					CustomerAccountManager customManager = new CustomerAccountManager();
					Customer aCustomer = customManager.getCustomer(request.getParameter("Email"));
					request.setAttribute("Firstname", aCustomer.getFirstname());	
					request.setAttribute("Lastname", aCustomer.getLastname());	
					request.setAttribute("Address", aCustomer.getAddress());	
					request.setAttribute("City", aCustomer.getCity());	
					request.setAttribute("Country", aCustomer.getCountry());	
					request.setAttribute("Phone", aCustomer.getPhone());	
					request.setAttribute("Company", aCustomer.getCompany());	
					request.setAttribute("Email", aCustomer.getEmail());	
					request.setAttribute("Password", aCustomer.getPassword());
					dispatcher = getServletContext().getRequestDispatcher("/PaymentJSP.jsp");		
					dispatcher.forward(request, response);

			}	
	}
}

		